import separateHeader from '@/components/separate-header.vue';
import topSwiper from '@/components/top-swiper/top-swiper.vue';
import productItemColumn from '@/components/product-item/components/product-item-column.vue'

import { fetchHotProduct } from '@/api/index.js';

export default {
	components: {
		separateHeader,
		topSwiper,
		productItemColumn
	},
	data() {
		return {
			productTopList:[],
			productList:[],
			pageParam: {
				pageNum: 0,
				pageSize: 10
			},
			loadingType: 'more',
		};
	},
	onLoad(options) {
		this.loadData('refresh');
	},
	//下拉刷新
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	//加载更多
	onReachBottom() {
		this.pageParam.pageNum++;
		this.loadData();
	},
	methods: {
		//加载商品 ，带下拉刷新和上滑加载
		async loadData(type = 'add', loading) {
			//没有更多直接返回
			if (type === 'add') {
				if (this.loadingType === 'nomore') {
					return;
				}
				this.loadingType = 'loading';
			} else {
				this.loadingType = 'more'
			}
		
			if (type === 'refresh') {
				this.pageParam.pageNum=0;
				this.productList = [];
			}
			fetchHotProduct(this.pageParam).then(response => {
				let pageData = response.data;
				//在原先list中追加数据
				this.productList = this.productList.concat(pageData.content);
				
				if(pageData.last){
					//没有更多了
					this.loadingType = 'nomore';
				}else{
					this.loadingType = 'more';
				}
				if (type === 'refresh') {
					//刷新才设置轮播
					if(this.productList.length > 3){
						this.productTopList = this.productList.slice(0,3);
						this.productList = this.productList.slice(3);
					}else{
						this.productTopList = this.productList;
					}
					
					if (loading == 1) {
						uni.hideLoading()
					} else {
						uni.stopPullDownRefresh();
					}
				}
			});
		},
	}
}